package com.my.kq.controller;

import com.my.kq.entity.Department;
import com.my.kq.entity.Employee;
import com.my.kq.entity.Position;
import com.my.kq.service.DepartmentService;
import com.my.kq.service.EmployeeService;
import com.my.kq.service.PositionService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Date;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@WebServlet("/EmployUpdatexg")
//修改
public class EmployUpdatexg extends HttpServlet {
    private EmployeeService EmployeeService=new EmployeeService();
    private PositionService positionService=new PositionService();
    private DepartmentService DepartmentService=new DepartmentService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            HttpSession session = req.getSession();
            Employee cook =(Employee) session.getAttribute("cook");
            //id
            Integer id = cook.getId();
            //工号
            String employeeNumber = req.getParameter("employeeNumber");
            //姓名
            String name = req.getParameter("name");
            //密码
            String password = req.getParameter("password");
            //性别
            String gender = req.getParameter("gender");
            //出生年月
            String date = req.getParameter("date");
            //电话号码
            String telephone = req.getParameter("telephone");
            //邮箱
            String email = req.getParameter("email");
            //籍贯
            String address = req.getParameter("address");
            //学历
            String education = req.getParameter("education");
            //部门
            String education1 = req.getParameter("education");
            //职称
            String educationc = req.getParameter("educationc");
            //状态
         //   String status = req.getParameter("status");
            //当前时间
            LocalDateTime time = LocalDateTime.now();
            DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String s = time.format(pattern);
            //备注
            String notes = req.getParameter("notes");

            int num=  EmployeeService.empUpdate(id,employeeNumber,name,password,gender,date,telephone,email,address,education,s,notes);
             //职称

            Position posi = (Position)session.getAttribute("posi");
            Integer posiId = posi.getId();
            positionService.posixg(educationc,posiId);


            //部门
            Department dep = (Department)session.getAttribute("dep");
            Integer number = dep.getDepartmentNumber();

            DepartmentService.depxg(number,education1);

            if(num>0){
                System.out.println("修改成功");
                req.getRequestDispatcher("EmployeeCx").forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
